package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.SettingAdapter;
import flc.ast.bean.SettingBean;
import flc.ast.databinding.DialogSettingBinding;
import stark.common.basic.base.BaseSmartDialog;

public class SettingDialog extends BaseSmartDialog<DialogSettingBinding> {

    private int mCurrentPos = 0;
    private List<SettingBean> mSettingBeans = new ArrayList<>();
    private SettingAdapter mSettingAdapter;

    public void setmCurrentPos(int mCurrentPos) {
        this.mCurrentPos = mCurrentPos;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    public SettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_setting;
    }

    @Override
    protected void initView(View view) {
        mSettingAdapter = new SettingAdapter();
        mDataBinding.rvDialogSettingList.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.rvDialogSettingList.setAdapter(mSettingAdapter);
        getData();
        mSettingAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mSettingAdapter.getItem(mCurrentPos).setSelected(false);
                mSettingAdapter.getItem(position).setSelected(true);
                mSettingAdapter.notifyDataSetChanged();
                mCurrentPos = position;
                if (listener != null) {
                    listener.clickItemName(mSettingAdapter.getItem(mCurrentPos).getSettingName(), position);
                }
                dismiss();
            }
        });
    }


    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }


    public interface IListener {
        //点击item
        void clickItemName(String name, int position);
    }

    private void getData() {
        mSettingBeans.add(new SettingBean(getContext().getString(R.string.setting_name1), false));
        mSettingBeans.add(new SettingBean(getContext().getString(R.string.setting_name2), false));
        mSettingBeans.add(new SettingBean(getContext().getString(R.string.setting_name3), false));
        mSettingBeans.add(new SettingBean(getContext().getString(R.string.setting_name4), false));
        mSettingBeans.get(mCurrentPos).setSelected(true);
    }

}
